/*Includes ----------------------------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "stm32f10x.h"
#include "misc.h"
#include "time.h"


/*Defines -----------------------------------------------------------------------------------------*/

/*Variables ---------------------------------------------------------------------------------------*/

/*Functions ---------------------------------------------------------------------------------------*/
/**
  * @brief  定时器1初始化
  * @param  arr 初值
  *         psc 分频系数
  * @retval 无
  */
void TIM1_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //定义结构体变量
  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //TIME1 时钟使能

  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //设置时钟分割:TDTS = Tck_tim
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;            //重复计数设置
  TIM_TimeBaseStructure.TIM_Period = arr;                     //初值重装
  TIM_TimeBaseStructure.TIM_Prescaler = psc;                  //预分频
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);             //根据上面的参数初始化定时器

  TIM_ClearFlag(TIM1, TIM_FLAG_Update); //清除中断标志位

  NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;        //TIM1中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;        //从优先级3级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;           //IRQ通道被使能
  NVIC_Init(&NVIC_InitStructure);                           //根据指定的参数初始化外设NVIC寄存器

  TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //TIM1使能中断
  TIM_Cmd(TIM1, ENABLE);                     //使能TIMx外设
}

